﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Work1_UI.Form1;

namespace Work1_UI
{
    class SelectPicture
    {
        int pictureCout = 0; //记录一次性输入多少张图片

        public SelectPicture()
        {
            string[] strPhoFilt; //图片路径数组
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Multiselect = true;  //对话框是否允许选择多个文件
            openFileDialog.Title = "请选择图像文件"; //弹窗的标题
            openFileDialog.InitialDirectory = "E:\\考核资料\\Halcon学习考核";  //默认打开的文件夹的初始位置
            //openFileDialog.ShowHelp = true;    //是否显示帮助按钮

            //图像文件
            openFileDialog.Filter = "图像文件(*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp";

            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                strPhoFilt = openFileDialog.FileNames;
                pictureCout = strPhoFilt.Length; //记录输入了多少张图片

                for (int i = 0; i < strPhoFilt.Length; i++)
                {
                    FileStream fileStream = new FileStream(strPhoFilt[i], FileMode.Open, FileAccess.Read); //使用流读取图片避免文件占用
                    //Form1.form1.imageList1.ColorDepth = ColorDepth.Depth32Bit;
                    Form1.form1.imageList1.Images.Add(Image.FromStream(fileStream)); //将获取到的图片添加至imageList

                    fileStream.Flush(); //清除缓冲区
                    fileStream.Close(); //关闭
                    fileStream.Dispose(); //释放
                }


                //使用委托添加图像
                Form1.form1.listView1.Invoke(new MethodInvoker(() =>
                {
                    Form1.form1.listView1.SmallImageList = Form1.form1.imageList1; //  SmallImageList 属性

                    for (int i = 0; i < pictureCout; i++) //显示imageList中新添加的图片
                    {
                        ListViewItem item = new ListViewItem();
                        item.ImageIndex = Form1.form1.imageList1.Images.Count - pictureCout + i;
                        item.Text = strPhoFilt[i]; //添加图片路径信息
                        Form1.form1.listView1.Items.Add(item);

                    }

                    Form1.form1.listView1.View = View.LargeIcon;
                    Form1.form1.listView1.LargeImageList = Form1.form1.imageList1;

                    pictureCout = 0; //复位

                    log.Info("读取图片");
                    form1.MessageShow(MessageType.Info, "读取图片");

                }));
            }


        }
    }



}
